from django.core.paginator import Paginator, PageNotAnInteger, InvalidPage, EmptyPage
from django.http import HttpResponse
from django.shortcuts import render, redirect, reverse
from .models import ActivityCourse, ActivityProjectDevelop, ActivityContest


# Create your views here.
def get_plan(request):
    courses = ActivityCourse.objects.all()
    project_develops = ActivityProjectDevelop.objects.all()
    contests = ActivityContest.objects.all()

    course_top3 = get_top3(courses)
    project_develop_top3 = get_top3(project_develops)
    contest_top3 = get_top3(contests)

    return render(request, 'activity/activity.html', {
        'course_top3': course_top3,
        'project_develop_top3': project_develop_top3,
        'contest_top3': contest_top3
    })


def get_top3(items):
    if items.count() < 3:
        top3 = list(items)
        while len(top3) < 3:
            top3.append({
                'title': '敬请期待',
                'abstract': '敬请期待'
            })
    else:
        top3 = items.order_by('create_time')[:3]

    return top3


def get_course_page(request):
    # 获取数据库所有的课程培训信息
    courses = ActivityCourse.objects.all()

    # 使用django的Paginator进行分页设计
    # 第一个参数为所有notice的对象，第二个参数为每页显示的通知数量
    paginator = Paginator(courses, 3)
    # 获取分页的总数
    page_num_all = paginator.num_pages

    # 获取url传过来的page信息，如果没有则默认赋值为1
    page_num = request.GET.get('page')
    try:
        current_page = paginator.page(page_num)
        page_num = int(page_num)
    # todo: 注意捕获异常
    except PageNotAnInteger:
        # 如果请求的页数不是整数, 返回第一页。
        current_page = paginator.page(1)
        page_num = 1
    except InvalidPage:
        # 如果请求的页数不存在, 重定向页面
        return HttpResponse('找不到页面的内容')
    except EmptyPage:
        # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
        current_page = paginator.page(page_num_all)
        page_num = page_num_all

    # 当前页面有没有下一页
    if current_page.has_next():
        next_page_num = page_num + 1
    else:
        next_page_num = page_num
    # 当前页面有没有上一页
    if current_page.has_previous():
        previous_page_num = page_num - 1
    else:
        previous_page_num = page_num
    return render(request, 'activity/course.html', {
        'page_num': page_num,
        'page_num_all': page_num_all,
        'page_num_all_range': range(1, page_num_all + 1),
        'current_page': current_page,
        'next_page_num': next_page_num,
        'previous_page_num': previous_page_num
    })


def get_course_detail(request):
    # 获取url传过来的article信息，如果没有则默认赋值为1
    global notice
    course_id = request.GET.get('article')

    try:
        course = ActivityCourse.objects.get(id=course_id)
    except:
        err = '文章索引超出范围'
        return redirect(reverse('activity'), {
            'err': err
        })

    return render(request, 'activity/details.html', {
        'course': course
    })


def get_project_develop(request):
    # 获取数据库所有的课程培训信息
    projectDevelop = ActivityProjectDevelop.objects.all()

    # 使用django的Paginator进行分页设计
    # 第一个参数为所有notice的对象，第二个参数为每页显示的通知数量
    paginator = Paginator(projectDevelop, 3)
    # 获取分页的总数
    page_num_all = paginator.num_pages

    # 获取url传过来的page信息，如果没有则默认赋值为1
    page_num = request.GET.get('page')
    try:
        current_page = paginator.page(page_num)
        page_num = int(page_num)
    # todo: 注意捕获异常
    except PageNotAnInteger:
        # 如果请求的页数不是整数, 返回第一页。
        current_page = paginator.page(1)
        page_num = 1
    except InvalidPage:
        # 如果请求的页数不存在, 重定向页面
        return HttpResponse('找不到页面的内容')
    except EmptyPage:
        # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
        current_page = paginator.page(page_num_all)
        page_num = page_num_all

    # 当前页面有没有下一页
    if current_page.has_next():
        next_page_num = page_num + 1
    else:
        next_page_num = page_num
    # 当前页面有没有上一页
    if current_page.has_previous():
        previous_page_num = page_num - 1
    else:
        previous_page_num = page_num
    return render(request, 'activity/projectDevelop.html', {
        'page_num': page_num,
        'page_num_all': page_num_all,
        'page_num_all_range': range(1, page_num_all + 1),
        'current_page': current_page,
        'next_page_num': next_page_num,
        'previous_page_num': previous_page_num
    })


def get_contest(request):
    # 获取数据库所有的课程培训信息
    contest = ActivityContest.objects.all()

    # 使用django的Paginator进行分页设计
    # 第一个参数为所有notice的对象，第二个参数为每页显示的通知数量
    paginator = Paginator(contest, 3)
    # 获取分页的总数
    page_num_all = paginator.num_pages

    # 获取url传过来的page信息，如果没有则默认赋值为1
    page_num = request.GET.get('page')
    try:
        current_page = paginator.page(page_num)
        page_num = int(page_num)
    # todo: 注意捕获异常
    except PageNotAnInteger:
        # 如果请求的页数不是整数, 返回第一页。
        current_page = paginator.page(1)
        page_num = 1
    except InvalidPage:
        # 如果请求的页数不存在, 重定向页面
        return HttpResponse('找不到页面的内容')
    except EmptyPage:
        # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
        current_page = paginator.page(page_num_all)
        page_num = page_num_all

    # 当前页面有没有下一页
    if current_page.has_next():
        next_page_num = page_num + 1
    else:
        next_page_num = page_num
    # 当前页面有没有上一页
    if current_page.has_previous():
        previous_page_num = page_num - 1
    else:
        previous_page_num = page_num
    return render(request, 'activity/contest.html', {
        'page_num': page_num,
        'page_num_all': page_num_all,
        'page_num_all_range': range(1, page_num_all + 1),
        'current_page': current_page,
        'next_page_num': next_page_num,
        'previous_page_num': previous_page_num
    })


def get_project_develop_detail(request):
    # 获取url传过来的article信息，如果没有则默认赋值为1
    global notice
    course_id = request.GET.get('article')

    try:
        course = ActivityProjectDevelop.objects.get(id=course_id)
    except:
        err = '文章索引超出范围'
        return redirect(reverse('activity'), {
            'err': err
        })

    return render(request, 'activity/details.html', {
        'course': course
    })


def get_contest_detail(request):
    # 获取url传过来的article信息，如果没有则默认赋值为1
    global notice
    course_id = request.GET.get('article')

    try:
        course = ActivityContest.objects.get(id=course_id)
    except:
        err = '文章索引超出范围'
        return redirect(reverse('activity'), {
            'err': err
        })

    return render(request, 'activity/details.html', {
        'course': course
    })
